#### TABLE A1: SAMPLE REPRESENTATIVENESS (STUDIES 1 & 2)
#### Proportion of Venezuelans by department

rm(list = ls())
source("./2_code/00_setup.R")

#### LOAD QUOTA DATA ####

cuotas <- read_excel(paste0(data_path, "muestra.xlsx"))
cuotas$Proportion <- cuotas$prop / 100


#### STUDY 1 ####

data <- fread(paste0(data_path, "data_study1.csv"), header = TRUE)

state_counts <- data %>%
  group_by(est.x) %>%
  summarise(count = n())

total_observations <- nrow(data)

state_proportions <- state_counts %>%
  mutate(proportion = count / total_observations)

state_proportions$offerwise_state <- state_proportions$est.x
state_proportions$proportion_study1 <- state_proportions$proportion


#### STUDY 2 ####

data2 <- fread(paste0(data_path, "data_study2.csv"), header = TRUE)

filtered_data2 <- data2 %>%
  filter(est != "")

state_counts_2 <- filtered_data2 %>%
  group_by(est) %>%
  summarise(count = n())

total_observations_2 <- nrow(filtered_data2)

state_proportions_2 <- state_counts_2 %>%
  mutate(proportion = count / total_observations_2)

state_proportions_2$dept <- state_proportions_2$est
state_proportions_2$proportion_study2 <- state_proportions_2$proportion


#### MERGE AND CREATE TABLE ####

merged_data <- left_join(cuotas, state_proportions[, c("offerwise_state", "proportion_study1")],
                         by = "offerwise_state")

final_merged_data <- left_join(merged_data, state_proportions_2[, c("dept", "proportion_study2")],
                               by = "dept")

final_data_for_latex <- final_merged_data %>%
  select(-prop, -offerwise_state) %>%
  rename(
    "Proportion Venezuelans" = Proportion,
    "Study 1" = proportion_study1,
    "Study 2" = proportion_study2,
    "Department" = dept
  )

final_data_for_latex <- final_data_for_latex %>%
  mutate(
    `Proportion Venezuelans` = sprintf("%.3f", `Proportion Venezuelans`),
    `Study 1` = sprintf("%.3f", `Study 1`),
    `Study 2` = sprintf("%.3f", `Study 2`)
  )


#### EXPORT TO LATEX ####

latex_table <- xtable(final_data_for_latex)

latex_output <- capture.output(print(latex_table, 
                                     include.rownames = FALSE, 
                                     sanitize.text.function = identity,
                                     include.colnames = TRUE, 
                                     floating = FALSE))

writeLines(latex_output, paste0(tables_path, "table_A1.tex"))
